package com.wenlong.train_ticketing_system_springboot.querymodule.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wenlong.train_ticketing_system_springboot.querymodule.pojo.Ticket;
import com.wenlong.train_ticketing_system_springboot.querymodule.pojo.User;
import com.wenlong.train_ticketing_system_springboot.querymodule.producter.RabbitProduct;
import com.wenlong.train_ticketing_system_springboot.querymodule.service.TicketService;
import com.wenlong.train_ticketing_system_springboot.querymodule.service.UserService;
import com.wenlong.train_ticketing_system_springboot.querymodule.websocketCom.WebSocketServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 苏文龙
 * @version 1.0
 */
@RestController
@RequestMapping("/trainT")
@Slf4j
public class InitializeController {
    @Autowired
    private UserService userService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitProduct rabbitProduct;

    @RequestMapping("/testInitialize")
    public String testReturnData() {
        return "hello, the first Time";
    }

    @RequestMapping("/")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", "wenlong");
        User user = userService.getOne(queryWrapper);
        request.getSession().setAttribute("userId", user.getId());
        response.setStatus(HttpServletResponse.SC_ACCEPTED);
        return ""+user.getId();
    }

    @RequestMapping("/getSessionData")
    public String getSessionData(HttpServletRequest request) {
        Object userId = request.getSession().getAttribute("userId");
        if (userId == null) return "the session do not have any data";
        return ""+ userId;
    }

    @RequestMapping("/testRedisContr")
    public List<Ticket> testRedisContr() {
        QueryWrapper<Ticket> queryWrapper = new QueryWrapper<>();
        List<Ticket> list = new ArrayList<>();
        queryWrapper.eq("price", "15");
        Ticket ticket = ticketService.getOne(queryWrapper);
        list.add(ticket);
        redisTemplate.opsForValue().set(String.valueOf(ticket.getId()), ticket);
        Ticket ticket1 = (Ticket) redisTemplate.opsForValue().get(String.valueOf(ticket.getId()));
        list.add(ticket1);
        return list;
    }

    @RequestMapping("/testWebSocket")
    public void testWebSocket(){
//        webSocketServerEndpoint.sendMessageOnline("hello, WebSocket!!~");
        log.info("====== log4j: the time now is {} =====", new Date());
        rabbitProduct.sendDelayMessageToNotify(4500, "the Message have delay 4500ms");
        rabbitProduct.sendDelayMessageToDeleteMsg(6000, "the Message have delay 6000ms");
    }
}
